import requests
from bs4 import BeautifulSoup
import os
tag="?h=5.15"
host="https://code.qt.io"
repname="qt/qtcharts.git"
reppath="https://code.qt.io/cgit/"+repname
outputpath="."
def getfile(pathf):
    print("get file:"+pathf)
    pathf=pathf.replace("tree","plain")
    print(pathf)
    res=requests.get(host+pathf)#"Classes/AppDelegate.h")
    ps=pathf.split("/")
    print(pathf)
    ps=ps[5:]
    filename=ps[-1].split("?")[0]
    ps[-1]=filename
    p="/".join(ps[:-1])
    p=outputpath+"/"+p
    if not os.path.exists(p):
        os.makedirs(p)
    open(p+"/"+filename,"wb").write(res.content)
def getpath(path):
    print("getpath:"+path)
    res=requests.get(host+path)
    soup = BeautifulSoup(res.content,"html.parser")
    # soup = BeautifulSoup(open("untitled.html","rb"),"html.parser")
    div_content=soup.find_all("div",attrs={"class":"content"})
    # print(div_content[0])
    # input("pause")
    tbs=div_content[0].find_all("table")
    # print(len(tbs))
    # input("pause")
    rs=tbs[0].find_all("tr")
    print(len(rs))
    fs=[]
    paths=[]
    for r in rs[1:]:
        # print(r)
        # input("show row")
        cs=r.find_all("td")
        print(len(cs))
        print(cs[0])
        if cs[1].a['class'][0]=='ls-blob':
            #is file
            fs.append(cs[1].a['href'])
        else:
            paths.append(cs[1].a['href'])
    # print(fs,paths)
    # input("befor next ")
    for f in fs:
        getfile(f)
    for p in paths:
        getpath(p)
def setrepname(nm):
    global repname
    global reppath
    global outputpath
    repname=nm
    # outputpath=nm.split("/")[1]
    # reppath="https://github.com/"+repname+"/tree"+tag
def main():
    # setrepname("qt/qtcharts.git")
    getpath("/cgit/qt/qtcharts.git/tree/examples/charts?h=5.15")#assets/js/vendor
if __name__=="__main__":
    main()